define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'upload'], function ($, undefined, Backend, Table, Form, Upload) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    del_url: 'user/user/del',
                    recharge_url: 'user/user/recharge',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'group.name', title: __('班级分组')},
                        {field: 'username', title: __('Username'), operate: 'LIKE'},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'email', title: __('Email'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        // {field: 'level', title: __('Level'), operate: 'BETWEEN', sortable: true},
                        {field: 'gender', title: __('Gender'), visible: false, searchList: {1: __('Male'), 0: __('Female')}},
                        // {field: 'score', title: __('Score'), operate: 'BETWEEN', sortable: true},
                        // {field: 'successions', title: __('Successions'), visible: false, operate: 'BETWEEN', sortable: true},
                        // {field: 'maxsuccessions', title: __('Maxsuccessions'), visible: false, operate: 'BETWEEN', sortable: true},
                        {field: 'logintime', title: __('Logintime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        // {field: 'loginip', title: __('Loginip'), formatter: Table.api.formatter.search},
                        {field: 'jointime', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'num_comments', title: __('点评次数')},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('Hidden')}},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,buttons: [
                                {
                                    name: 'usercourse',
                                    title: __('编辑我的课程'),
                                    text:'我的课程',
                                    extend:'data-area=["100%","100%"]',
                                    classname: 'btn btn-xs btn-default btn-dialog',
                                    icon: 'fa fa-bars',
                                    url: 'user/yalimedia_course_user/index'
                                },
                                
                                {
                                    name: 'principal',
                                    extend:'data-area=["90%","90%"]',
                                    title: __('增加点评次数'),
                                    text:'点评次数',
                                    classname: 'btn btn-xs btn-success btn-warning btn-dialog',
                                    icon: 'fa',
                                    url: 'user/user_history/index'
                                }
                                // {
                                //     name: 'principal',
                                //     title: __('增加点评次数'),
                                //     text:'点评次数',
                                //     extend:'data-area=["50%","50%"]',
                                //     classname: 'btn btn-xs btn-default btn-dialog',
                                //     icon: 'fa fa-plus-square',
                                //     url: 'user/user/recharge',
                                //     callback: (data) => {

                                //         console.log(data)
                                //         return false

                                //         Fast.api.ajax({
                                //             url: 'yalimedia/course/add_question',
                                //             data: {
                                //                 'id':id
                                //             },
                                //             success: function(data) {
                                //                 Layer.closeAll();
                                //                 Toastr.success('添加成功');
                                //             }
                                //         });
                                //     }
                                // }
                            ], formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            // 打开导入页面
            $('.btn-importPage').click(function () {
                Fast.api.open('user/user/import', '导入学员', {
                    area: ['800px', '380px'],
                    callback: function (data) {
                        table.bootstrapTable('refresh');
                    }
                });
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        recharge(){
            Controller.api.bindevent();
        },
        import: function () {

            Controller.api.bindevent();
            
            // 手动绑定导入事件
            Upload.api.upload($('.btn-import'), function (data, ret) {
                let cate = $('#c-cate_id').val();
                
                if (!cate) { //  || !exam_type
                    Fast.api.msg('请先选择班级分组再进行上传！')
                    return false;
                }
                
                $('#file_url').val(data.url);
                Fast.api.ajax({
                    url: 'user/user/importExcel',
                    data: {
                        file: data.url,
                        cate: cate,
                        // exam_type: exam_type
                    },
                }, function (data, ret) {
                    console.log(data, ret)
                    $('#question_count').html(`本次上传识别到 ${data.count} 学员`)
                });
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});